package com.pheruagor.handmanager.view {
    import com.pheruagor.handmanager.controller.CloseMemberEditionPopUpCommand;
    import com.pheruagor.handmanager.controller.OpenMemberEditionPopUpCommand;
    import com.pheruagor.handmanager.model.ClubViewerProxy;
    import com.pheruagor.handmanager.model.business.Member;
    import com.pheruagor.handmanager.model.business.event.ValidationEvent;
    import com.pheruagor.handmanager.view.component.ClubViewer;
    import com.pheruagor.handmanager.view.component.MemberCard;
    import com.pheruagor.handmanager.view.component.ValidationPopUp;

    import flash.events.Event;
    import flash.events.MouseEvent;

    import mx.core.ClassFactory;
    import mx.events.FlexEvent;
    import mx.managers.PopUpManager;
    import mx.utils.StringUtil;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class ClubViewerMediator extends Mediator {
        public static const NAME:String = "clubViewerMediator";

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_clubViewerProxy:ClubViewerProxy;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ClubViewerMediator(view:ClubViewer) {
            super(NAME, view);
        }

        //-----------------------------------------------------
        // IMediator Interface
        //-----------------------------------------------------
        override public function listNotificationInterests():Array {
            return [CloseMemberEditionPopUpCommand.EXECUTED];
        }

        override public function handleNotification(notification:INotification):void {
            switch (notification.getName()) {
                case CloseMemberEditionPopUpCommand.EXECUTED:
                    var member:Member = Member(notification.getBody());
                    if (member != null) {
                        m_clubViewerProxy.saveMember(member);
                    }
                    break;
            }
        }

        override public function onRegister():void {
            m_clubViewerProxy = ClubViewerProxy(facade.retrieveProxy(ClubViewerProxy.NAME));
            clubViewer.addEventListener(FlexEvent.CREATION_COMPLETE, onClubViewerCreationCompleted);
        }

        override public function onRemove():void {
            clubViewer.addMemberButton.removeEventListener(MouseEvent.CLICK, onAddMemberButtonClicked);
            clubViewer.removeEventListener(FlexEvent.CREATION_COMPLETE, onClubViewerCreationCompleted);
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get clubViewer():ClubViewer {
            return ClubViewer(viewComponent);
        }

        //-----------------------------------------------------
        // Event handler
        //-----------------------------------------------------
        private function onClubViewerCreationCompleted(event:FlexEvent):void {
            var proxy:ClubViewerProxy = ClubViewerProxy(facade.retrieveProxy(ClubViewerProxy.NAME));
            clubViewer.title = "Club " + proxy.currentClub.name;
            clubViewer.memberList.dataProvider = proxy.currentClub.members;
            clubViewer.memberList.itemRenderer = new ClassFactory(MemberCard);
            clubViewer.addMemberButton.addEventListener(MouseEvent.CLICK, onAddMemberButtonClicked);
            clubViewer.memberList.addEventListener(MemberCard.EDIT, onMemberEdited);
            clubViewer.memberList.addEventListener(MemberCard.REMOVE, onMemberRemoved);
        }

        private function onMemberEdited(event:Event):void {
            var memberCard:MemberCard = MemberCard(event.target);
            facade.sendNotification(OpenMemberEditionPopUpCommand.NAME, {"container": clubViewer, "member": memberCard.data});
        }

        private function onMemberRemoved(event:Event):void {
            var memberCard:MemberCard = MemberCard(event.target);
            var member:Member = Member(memberCard.data);
            var onValidationOk:Function = function(event:Event):void {
                    m_clubViewerProxy.removeMember(member);
                    PopUpManager.removePopUp(popup);
                    facade.removeMediator(ValidationPopUpMediator.NAME);
                }
            var onValidationCancel:Function = function(event:Event):void {
                    PopUpManager.removePopUp(popup);
                    facade.removeMediator(ValidationPopUpMediator.NAME);
                }
            var popup:ValidationPopUp = new ValidationPopUp();
            var mediator:ValidationPopUpMediator = new ValidationPopUpMediator(popup);
            facade.registerMediator(mediator);
            PopUpManager.addPopUp(popup, memberCard);
            PopUpManager.centerPopUp(popup);
            mediator.validationTitle = "Suppression d'un membre";
            mediator.validationLabel = StringUtil.substitute("Etes vous sur de vouloir supprimer le member '{0} {1}'?", member.forname
                , member.lastname);
            popup.addEventListener(ValidationEvent.OK, onValidationOk);
            popup.addEventListener(ValidationEvent.CANCEL, onValidationCancel);
        }

        private function onAddMemberButtonClicked(event:MouseEvent):void {
            facade.sendNotification(OpenMemberEditionPopUpCommand.NAME, {"container": clubViewer, "member": null});
        }

        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------

        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------

    }
}